package com.yxy.service.Impl;



import com.github.pagehelper.PageHelper;
import com.yxy.dao.SportDynamicDao;
import com.yxy.entity.SportDynamic;
import com.yxy.service.SportDynamicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;


/**
 *
 * SportDynamicServiceImpl 实现服务类
 *
 * @version
 *
<pre>
 * Author       Version         Date            Changes
 * 邓文俊    1.0  2018-01-11 16:43:07	 	Created
 *
 * </pre>
 * @since 1.
 */
@Service
@Transactional
public class SportDynamicServiceImpl implements SportDynamicService {

	private Logger logger = LoggerFactory.getLogger(SportDynamicServiceImpl.class);

	@Autowired
	private SportDynamicDao sportDynamicDao;


	public boolean create(SportDynamic sportDynamic){
		logger.info("单个新增：{}",sportDynamic);
		int count = sportDynamicDao.insert(sportDynamic);
		return count > 0;
	}


	public boolean update(SportDynamic entity){
		logger.info("单个更新：{}", entity);
		int count = sportDynamicDao.updateByPrimaryKey(entity);
		return count > 0;
	}


	public boolean delete(SportDynamic sportDynamic){
		logger.info("单个删除：{}", sportDynamic);
		int count = sportDynamicDao.deleteByPrimaryKey(sportDynamic);
		return count > 0;
	}

	public SportDynamic findByPK(Integer id){
		SportDynamic entity = new SportDynamic();
		entity.setId(id);
		logger.info("单个查询：{}", entity);
		SportDynamic sportDynamic = sportDynamicDao.select(entity);
		return sportDynamic;
	}


	public List<SportDynamic> findAll(){
		SportDynamic  entity = new  SportDynamic();
		logger.info("查询所有：{}", entity);
		return sportDynamicDao.selectAll(entity);
	}









	public boolean deleteAll(List<SportDynamic> list){
		logger.info("批量删除：{}", list);
		int count = sportDynamicDao.deleteBatch(list);
		return count > 0;
	}





	public boolean addBatch(List<SportDynamic> list){
		logger.info("批量添加数据：{}",list);
		int count = sportDynamicDao.insertBatch(list);
		return count > 0;
	}

	public List<SportDynamic> findLikeList(SportDynamic sportDynamic){
		return sportDynamicDao.selectAll(sportDynamic);
	}

	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public  List<SportDynamic> findPage(Integer pageNum,Integer pageSize){
		SportDynamic  entity = new SportDynamic();
		logger.info("分页查询：{}");
		PageHelper.startPage(pageNum, pageSize);
		return sportDynamicDao.selectAll(entity);
	}

	public boolean SportDynamicDoLike(Integer userId,Integer sportDynamicId){
		int result = sportDynamicDao.selectSportDynamicLikeRelationship(userId,sportDynamicId);
		SportDynamic sportDynamic = new SportDynamic();
		sportDynamic.setId(sportDynamicId);
		SportDynamic oldSportDynamic = sportDynamicDao.select(sportDynamic);//查询出数据库的记录信息
		int count =0;
		if(result>0){		//如果已经存在记录，说明已经点赞
			count += sportDynamicDao.deleteSportDynamicLikeRelationship(userId,sportDynamicId);//取消点赞
			oldSportDynamic.setSportDynamicLikesNum(oldSportDynamic.getSportDynamicLikesNum()-1);
		}else {
			count +=sportDynamicDao.insertSportDynamicLikeRelationship(userId,sportDynamicId);//点赞
			oldSportDynamic.setSportDynamicLikesNum(oldSportDynamic.getSportDynamicLikesNum()+1);
		}
		count +=sportDynamicDao.updateByPrimaryKey(oldSportDynamic);
		return count>0;
	}

	public List<SportDynamic> selectMySportDynamic(Integer userId){
		SportDynamic sportDynamic = new SportDynamic();
		sportDynamic.setUserId(userId);
		return sportDynamicDao.selectAll(sportDynamic);
	}
}